package com.lonely.fx.property;

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

/**
 * @date: 2022-07-21 17:58
 * @author: ztkj-hzb
 * @description: 针对 String类型的 监听包装
 **/
public class StringPropertySettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        GridPane gridPane = new GridPane();
        gridPane.setVgap(20);
        gridPane.setHgap(20);

        Label label = new Label("请输入待修改的值");
        TextField textField = new TextField();
        Button button = new Button("确定");
        TextArea textArea = new TextArea();
        textArea.setStyle("-fx-background-color: pink");

        gridPane.add(label, 0, 0);
        gridPane.add(textField, 1, 0);
        gridPane.add(button, 2, 0);
        gridPane.add(textArea, 0, 1, 3, 1);


        //第一种方式 SimpleStringProperty ---> 可读可写
        SimpleStringProperty simpleStringProperty = new SimpleStringProperty();
        simpleStringProperty.set("hello");

        //第二种方式 ReadOnlyStringProperty --> 只读，没有提供set方法
        ReadOnlyStringWrapper readOnlyStringWrapper = new ReadOnlyStringWrapper("风中追风");
        ReadOnlyStringProperty readOnlyProperty = readOnlyStringWrapper.getReadOnlyProperty();
        String value = readOnlyProperty.get();
        System.out.println(value);


        //事件
        button.setOnAction(event -> {
            String text = textField.getText();
            if (text == null || text.length() == 0) {
                return;
            }
            simpleStringProperty.set(text);
        });


        simpleStringProperty.addListener((observable, oldValue, newValue) -> textArea.setText(newValue));

        primaryStage.setScene(new Scene(gridPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
